<?php

namespace App\Http\Controllers\Tool;

use Illuminate\Http\Request;

use App\Http\Requests;
use Redirect, Input, Response;
use App\Http\Controllers\Controller;
use zgldh\QiniuStorage\QiniuStorage;
use App\Models\Attachs;
use EndaEditor;
class UploadController extends Controller
{
    public function uploadImg(){
   
     $file = Input::file('file');
            $disk = QiniuStorage::disk('qiniu');
            
            if($file){
                $allowed_extensions = ["png", "jpg", "gif"];
                if ($file->getClientOriginalExtension() && !in_array($file->getClientOriginalExtension(), $allowed_extensions)) {
                    return ['error' => 'You may only upload png, jpg or gif.'];
                }
                $imagesName=  file_get_contents($file->getRealPath());
                
                $file_name=$this->check(md5($file->getClientOriginalName().time()), $file->getClientOriginalExtension());
                $disk->put($file_name, $imagesName);
                $attach_data=array(
                    'name'=>$file_name,
                    'path'=>$disk->imagePreviewUrl($file_name,''),
                    'exts'=>$file->getClientOriginalExtension(),
                    'addtime'=>time(),
                );
                $attach =Attachs::create($attach_data);
                return Response::json(array('success' =>true,'url' =>$disk->imagePreviewUrl($file_name,''),'id' => $attach->id) );
           }elseif(Input::get('base64')){
                $image_base64 = Input::get('base64');
                $file_names = Input::get('filename');
                if(!$file_names){
                    if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $image_base64, $result)){
                        $ext = $result[2];
                        $image_base64 = str_replace('data:image/'.$ext.';base64,', '', $image_base64);
                    }else {
                        return Response::json(array('success' => false));
                    }
                }else{
                    $file_arr = explode('.', $file_names);
                    $ext = end($file_arr);
                    $image_base64 = str_replace('data:image/jpeg;base64,', '', $image_base64);
                }

                $file=base64_decode($image_base64);
                $file_name=$this->check(md5($image_base64.time()),$ext);
                $disk->put($file_name,$file);
                $attach_data=array(
                    'name'=>$file_name,
                    'path'=>$disk->imagePreviewUrl($file_name,''),
                    'exts'=>$ext,
                    'addtime'=>time(),
                );
                $attach =Attachs::create($attach_data);
                return Response::json(array('success'=>true,'url'=>$disk->imagePreviewUrl($file_name,''),'id' => $attach->id));
            }else {
                return Response::json(array('success'=>false));
            }
    }
    
    
    public function index(){
        return view('tool.upload.index');
    }
    
    
    public function uploadEditor(){
        $data = EndaEditor::uploadImgFile('path');
        return json_encode($data);
    }
    

    public function check($name,$ext){
        $disk = QiniuStorage::disk('qiniu');
        if($disk->exists($name.'.'.$ext)){
            $name=md5($name.time());
            $this->check($name,$ext);
        }
        return $name.'.'.$ext;
    }


    public function imageUrl(){
        $file = Input::file('file');
        if($file){
            $allowed_extensions = ["png", "jpg", "gif"];
            if ($file->getClientOriginalExtension() && !in_array($file->getClientOriginalExtension(), $allowed_extensions)) {
                return ['error' => 'You may only upload png, jpg or gif.'];
            }
            $imagesName=  file_get_contents($file->getRealPath());

            $file_name=$this->check(md5($file->getClientOriginalName().time()), $file->getClientOriginalExtension());
            $this->disk->put($file_name, $imagesName);
            return Response::json(array('success' =>true,'url' =>$this->_disk->imagePreviewUrl($file_name,'')) );
        }elseif(Input::get('base64')){
            $image_base64 = Input::get('base64');
            $file_names = Input::get('filename');
            if(!$file_names){
                if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $image_base64, $result)){
                    $ext = $result[2];
                    $image_base64 = str_replace('data:image/'.$ext.';base64,', '', $image_base64);
                }else {
                    return Response::json(array('success' => false));
                }
            }else{
                $file_arr = explode('.', $file_names);
                $ext = end($file_arr);
                $image_base64 = str_replace('data:image/jpeg;base64,', '', $image_base64);
            }
            $file=base64_decode($image_base64);
            $file_name=$this->check(md5($image_base64.time()),$ext);
            $this->_disk->put($file_name,$file);
            return Response::json(array('success'=>true,'url'=>$this->_disk->imagePreviewUrl($file_name,'')));
        }else{
            return Response::json(array('success'=>false));
        }
    }

}
